home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / system / idcutils.zip / CHKDRV.ASM < prev    next >
Assembly Source File  |  1990-05-20  |  7KB  |  297 lines

  1.  
  2. ; Program    : CHKDRV.ASM
  3. ; Author    : Infinity Design Concepts Inc. Louisville, Ky 40217-2333
  4. ;        : Voice: (502)-636-1234   Data: (502)635-5471
  5. ;          Written by Gary Conway
  6. ; Created    : 5/20/90
  7. ; Update    : 
  8. ; Version    : 1.0
  9. ; Notice    :
  10. ; MASM 5.1 format
  11.  
  12.  
  13. COMMENT    |
  14.  
  15.     This program will test the drive letter given on the command line
  16.     and return an ERRORLEVEL (testable in BATch files).
  17.  
  18.     ERRORLEVEL return values
  19.     
  20.         0 - specified drive is ready
  21.         1 - specified drive is NOT ready
  22.  
  23.     A sample batch file would be constructed as follows,
  24.  
  25.     ECHO OFF
  26.     REM checking drive A:
  27.     CHKDRV A
  28.     IF ERRORLEVEL 1 GOTO NOTREADY
  29.     ECHO   Drive A: is READY
  30.     GOTO END
  31.     :NOTREADY
  32.     ECHO   Drive A: is NOT READY
  33.     GOTO END
  34.     :END
  35.     
  36.  
  37.  
  38.     |
  39.  
  40.  
  41. NOTREADY    EQU    1
  42. CR        EQU    13
  43. TAB        EQU    9
  44.  
  45. CSEG        SEGMENT PARA PUBLIC 'CODE'
  46.         ASSUME CS:CSEG,DS:DSEG,ES:DSEG,SS:STACK
  47. START:
  48.         Mov    AX,DSEG         ; set up segment addresses
  49.         Mov    DS,AX            ; data seg.
  50.         Mov    [PSP],ES        ; save program segment prefix
  51.         Mov    ES,AX            ; extra seg.
  52.  
  53.         Call    Install_Vectors        ; take control of ^C and ^BREAK
  54.                         ; and critical error ints.
  55.         Call    GetParms        ; get command line parms
  56.         Mov    AL,[Parm1]        ; get upcase drive letter
  57.         Mov    AH,36h
  58.         Mov    DL,AL            ; A: = 1
  59.         Sub    DL,40h
  60.         Int    21h            ; get disk space
  61.         Jc    _not_ready
  62.         Cmp    AX,-1            ; drive letter invalid ?
  63.         Je    _not_ready
  64.         Cmp    CS:[Crit_Error],0    ; disk error ?
  65.         Je    Exit            ; nope
  66.  
  67. _not_ready:    Mov    [ErrorLevel],1        ; drive not ready
  68.  
  69. Exit:        Call    Remove_Vectors        ; restore old vectors as
  70.                         ; we found them
  71.  
  72.         Mov    AH,4Ch
  73.         Mov    AL,[ErrorLevel]        ; return drive ready or not
  74.                         ; as ERRORLEVEL
  75.         Int    21h            ; return to DOS
  76.  
  77.  
  78. ;=========================================================================
  79. ; convert the character in AL to uppercase
  80. ;=========================================================================
  81. ;
  82. Case        Proc    Near        ; convert AL to upper case
  83.         Cmp    AL,'a'
  84.         JL    GBACK
  85.         Cmp    AL,'z'
  86.         JG    GBACK
  87.         Sub    AL,20H
  88. GBACK:        Ret
  89. Case        Endp
  90.  
  91.  
  92. ;==========================================================
  93. ; get command line parameters from PSP
  94. ; the command line has the following structure,
  95. ; 80h = number of bytes in command line, excluding the <CR>
  96. ; 81h = start of command line, terminated by <CR>
  97. ;
  98. ; up to 4 command line arguments are supported and stored
  99. ; to PARM1-PARM4
  100. ;==========================================================
  101. ;
  102. GetParms    Proc
  103.         Push    DS
  104.         Mov    SI,81h
  105.         Mov    DS,ES:[PSP]
  106.         Lea    DI,Parm1
  107. Gmore:        Push    DI
  108. Lmore:        Lodsb
  109.         Cmp    AL,CR            ; end of command line?
  110.         Je    Mend            ; yes, done
  111.         Cmp    AL," "            ; bypass spaces
  112.         Je    Lmore
  113.         Cmp    AL,TAB            ; bypass TAB chars
  114.         Je    Lmore
  115.         Dec    SI
  116. Lmore1:        Lodsb
  117.         Cmp    AL,CR            ; CR ?
  118.         Je    Mend            ; yes, all done
  119.         Cmp    AL," "
  120.         Je    Lend            ; get next parm
  121.         Cmp    AL,TAB
  122.         Je    Lend            ; get next parm
  123.         Call    Case
  124.         Stosb
  125.         Jmp    Short Lmore1
  126. Lend:        Pop    DI
  127.         Add    DI,ParmLen
  128.         Jmp    Short Gmore
  129. Mend:        Pop    DI
  130.         Pop    DS
  131.         Ret
  132. GetParms    Endp
  133.  
  134.  
  135.  
  136.  
  137.  
  138. ;------------------------------------------------------------------------------
  139. ; install our own vector handlers
  140. ;
  141. ; MAKE SURE THAT YOU CALL INSTALL_VECTORS BEFORE YOU CALL REMOVE_VECTORS
  142. ; NOT DOING SO WILL CAUSE ERRATIC DOS BEHAVIOR GUARANTEED
  143. ;
  144. ;    This routine will replace the DOS interrupts as follows
  145. ;
  146. ;    INT 23    - ^C handler
  147. ;    INT 1B    - ^BREAK handler
  148. ;    INT 24    - critical error handler (Abort, Retry, Ignore)
  149. ;
  150. ;    It is useful to intercept these vectors so that, for example,
  151. ;    if the user were entering text and pressed ^C, your program will
  152. ;    not abort out to DOS in an unknown condition, yet your program
  153. ;    still retains the ability to check for a break condition by 
  154. ;    checking the CS:[Break_Flag] at appropriate points in your code
  155. ;    You may also check the CS:[Crit_Error] flag for critical disk errors
  156. ;    when reading files etc.
  157. ;
  158. ;    If you do want to make use of these two flags, then somewhere
  159. ;    near the beginning of your code, CALL INSTALL_VECTORS and then
  160. ;    when you exit or otherwise leave the main process, CALL REMOVE_VECTORS
  161. ;    to replace the original vectors as they arrived from DOS.
  162. ;
  163. ;    Process:
  164. ;
  165. ;    Both the ^C and ^BREAK interrupt vectors are replaced with
  166. ;    pointers to the same routine DUMMY_INT, which resides in our code
  167. ;    Whenever either of these key combinations is detected, CS:BREAK_FLAG
  168. ;    is set to a 1. All you need do is check this flag from time to time
  169. ;
  170. ;    The DOS critical error handler is replaced with a pointer to
  171. ;    our handler CRITICALERROR, which simply sets the CS:CRIT_ERROR flag
  172. ;    to a 1.
  173. ;
  174. ;-----------------------------------------------------------------------------
  175.  
  176. Install_Vectors    Proc
  177.         Push    ES
  178.         Mov    AX,3524h        ; get critical error vector
  179.         Int    21h            ; ES:BX returns with vector
  180.         Mov    CS:[Int24_SEG],ES    ; save it for restore later
  181.         Mov    CS:[Int24_Off],BX
  182.  
  183. ; save Int 1Bh (Ctrl-Break)
  184.         Mov    AX,351Bh
  185.         Int    21h            ; get existing vector
  186.         Mov    CS:[Int1B_Seg],ES
  187.         Mov    CS:[Int1B_Off],BX
  188.         
  189. ; save Int 23h (Ctrl-C)
  190.         Mov    AX,3523h
  191.         Int    21h            ; get existing vector
  192.         Mov    CS:[Int23_Seg],ES
  193.         Mov    CS:[Int23_Off],BX
  194.         Pop    ES
  195.  
  196. ; now set Int 24h vector pointing to our critical routine
  197.  
  198.         Push    DS
  199.         Push    CS
  200.         Pop    DS
  201.         Mov    AX,2524h        ; set function
  202.         Mov    DX,Offset CS:CriticalError
  203.         Int    21h
  204.  
  205. ; set up Int 1Bh with pointer to IRET
  206.  
  207.         Mov    DX,Offset CS:Dummy_Int
  208.         Mov    AX,251Bh
  209.         Int    21h            ; install ptr
  210.  
  211. ; set up Int 23h with pointer to IRET
  212.  
  213.         Mov    DX,Offset CS:Dummy_Int
  214.         Mov    AX,2523h
  215.         Int    21h            ; install ptr
  216.  
  217.         Pop    DS
  218.         Ret
  219. Install_Vectors    Endp
  220.  
  221.  
  222. ; remove IDC's error handlers from vector list and reinstall DOS vectors
  223. ;
  224. ; NOTE: MAKE SURE THAT YOU HAVE CALLED INSTALL_VECTORS BEFORE YOU
  225. ;    CALL REMOVE_VECTORS
  226.  
  227. Remove_Vectors    Proc
  228.         Push    DS
  229.         Mov    DS,CS:[Int24_SEG]
  230.         Mov    DX,CS:[Int24_Off]
  231.         Mov    AX,2524h        ; set critical error vector
  232.         Int    21h            ; back to DOS's
  233.  
  234.         Mov    DS,CS:[Int23_Seg]
  235.         Mov    DX,CS:[Int23_Off]
  236.         Mov    AX,2523h
  237.         Int    21h            ; restore original
  238.         
  239.         Mov    DS,CS:[Int1B_Seg]
  240.         Mov    DX,CS:[Int1B_Off]
  241.         Mov    AX,251Bh
  242.         Int    21h
  243.         Pop    DS
  244.         Ret
  245. Remove_Vectors    Endp
  246.  
  247.  
  248.  
  249. Dummy_Int    Proc    Far
  250.         Mov    CS:[Break_Flag],1    ; flag break
  251.         Iret
  252. Dummy_Int    Endp
  253.  
  254.  
  255. ; set CritError to non-zero error code if error has occurred
  256. ; SS,SP,DS,ES,BX,CX,DX must be preserved
  257. ; user stack in force
  258.  
  259. CriticalError    Proc    Far
  260.         Mov    CS:[Crit_Error],1    ; flag the error
  261.         Xor    AX,AX            ; tell DOS to ignore error
  262.         Iret
  263. CriticalError    Endp
  264.  
  265.  
  266. Break_Flag    DB    0    ; 1 then ^C or ^BREAK has been hit
  267. Crit_Error    DB    0    ; 1 then critical error has occured
  268. Int24_SEG    DW    0    ; save old vectors here
  269. Int24_Off    DW    0
  270. Int1B_Seg    DW    0
  271. Int1B_Off    DW    0
  272. Int23_Seg    DW    0
  273. Int23_Off    DW    0
  274.  
  275. CSEG        ENDS
  276.  
  277.  
  278. DSEG        SEGMENT PARA PUBLIC 'DATA'
  279. PSP        DW    0        ; program segment prefix
  280.  
  281. Parm1        DB    64 dup(0)
  282. Parm2        DB    64 dup(0)
  283. Parm3        DB    64 dup(0)
  284. Parm4        DB    64 dup(0)
  285. ParmLen        Equ    $ - Parm4
  286.  
  287.  
  288. ErrorLevel    DB    0        ; 0 = drive ready, 1 = not ready
  289. DSEG        ENDS
  290.  
  291.  
  292. STACK        SEGMENT PARA STACK 'STACK'
  293.         DW    500 DUP(0)
  294. STACK        ENDS
  295.  
  296.         END    START
  297.